﻿using System.Linq;
using System.IO;
using Algoritmia.Utils;
using Lists = Algoritmia.DataStructures.Lists;
using Algoritmia.DataStructures.Maps;

namespace Algoritmia.Data
{
    public class HyphenationMap : HashMap<int, IMap<string, Lists.IList<int>>>
    {
        public HyphenationMap(string rulesFile)
        {
            for (var i = 1; i < 9; i++)
            {
                this[i] = AlgoritmiaFactory.Maps<string, Lists.IList<int>>.Default;
            }
            using (var sr = new StreamReader(rulesFile))
            {
                while (!sr.EndOfStream)
                {
                    var line = sr.ReadLine();
                    if (line == null)
                    {
                        break; 
                    }
                    var splitted = line.Trim().Split(' ');
                    var ngram = splitted[0];
                    this[ngram.Length][ngram] =
                        splitted.Skip(1).Where(e => !string.IsNullOrEmpty(e)).Select(e => int.Parse(e)).
                            ToAlgoritmiaList();
                }
            }
        }

        public static HyphenationMap Default
        {
            get { return new HyphenationMap(@"..\..\..\Algoritmia\Data\SpanishHyphenationTable.txt"); }
        }

    }
}
